﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_RegUserCtl : UserControl, IRegUserView
{
   

    #region IRegUserView Members

    public RegUserPresenter Presenter { get; set; }

    public string LoginId
    {
        get { return loginIdTextBox.Text; }
    }

    public string CreatingUser()
    {
        MembershipCreateStatus membershipCreateStatus;

        Membership.CreateUser(loginIdTextBox.Text, ViewState["password"].ToString(), EmailTextBox.Text,
                              QuestionTextBox.Text, AnswerTextBox.Text, true, out membershipCreateStatus);

        if (membershipCreateStatus != MembershipCreateStatus.Success)
        {
            throw new Exception("Create user fail " + membershipCreateStatus);
        }
        User user = Presenter.GetUser(loginIdTextBox.Text);
        user.Name = ShowNameTextBox.Text;
        return loginIdTextBox.Text;
    }

    #endregion

    public override void DataBind()
    {
        base.DataBind();
        bool visible = Membership.RequiresQuestionAndAnswer;
        QuestionTextBox.Visible = visible;
        questionLabel.Visible = visible;
        QuestionRequiredFieldValidator.Visible = visible;

        AnswerTextBox.Visible = visible;
        answerLabel.Visible = visible;
        AnswerRequiredFieldValidator.Visible = visible;
    }

    protected void ValidateDuplicateName(object source, ServerValidateEventArgs args)
    {
        args.IsValid = !Presenter.IsExist(args.Value.Trim());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new RegUserPresenter(this);
        if (!IsPostBack)
        {
            Presenter.InitilizeView();
        }
        Presenter.ViewLoad();

        if (PasswordTextBox.Text.Length != 0)
            ViewState["password"] = PasswordTextBox.Text;
    }


    protected void ValidDuplicateEmail(object source, ServerValidateEventArgs args)
    {
        args.IsValid = !Presenter.IsDupliacteEmail(args.Value.Trim());
    }
}